package com.letv.app.ljodfh.ui.activities;

import android.os.Bundle;
import android.support.v4.util.ArrayMap;
import android.text.TextUtils;
import android.view.View;

import com.jiongbull.jlog.JLog;
import com.letv.app.ljodfh.base.Constances;
import com.mob.tools.utils.DeviceHelper;
import com.ruihang.generalibrary.ui.widget.CustomFontEditText;
import com.letv.app.ljodfh.R;
import com.letv.app.ljodfh.base.BaseActivity;
import com.letv.app.ljodfh.beans.SingleResult;
import com.letv.app.ljodfh.http.AbsNetCallBack;
import com.letv.app.ljodfh.http.DMethods;
import com.letv.app.ljodfh.http.NetUtils;
import com.letv.app.ljodfh.ui.utils.KeyBoardUtils;
import com.letv.app.ljodfh.ui.utils.TitleUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class InvitationCodeActivity extends BaseActivity {
    @BindView(R.id.invitation_code_number)
    protected CustomFontEditText invitation_code;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        new TitleUtils(this).setBackVisible(true).setLogo(false);
    }

    @Override
    protected void initInject() {
        setContentView(R.layout.activity_invitation_code);
        ButterKnife.bind(this);
    }

    @OnClick({R.id.invitation_code_submit})
    public void submitClick(View view) {
        String code = invitation_code.getText().toString();
        upLoadInvitationCode(code);
    }


    private void upLoadInvitationCode(String code) {
        KeyBoardUtils.closeKeyboard(this);
        showProgress(getString(R.string.being_submit), false);
        ArrayMap<String, Object> map = new ArrayMap<>();
        String did =
                DeviceHelper.getInstance(this).getDeviceId();
        JLog.e("did=" + did);
        map.put("deviceId", did);
        map.put("inviteCode", code);
        map.put("siteNumber", Constances.SITE_ID);
        NetUtils.getNetAdapter().postGetSingle(getOwnerName(), DMethods.SendCode, map, new AbsNetCallBack<SingleResult>(null) {
            @Override
            public void onSuc(SingleResult s) {
                closeProgress();
                if (s != null && s.isSuccess()) {
                    toast(getString(R.string.submit_success));
                    finish();
                } else {
                    toast(R.string.submit_failed);
                }
            }

            @Override
            public void onError(Throwable ex, String errorMsg) {
                super.onError(ex, errorMsg);
                if (ex != null) {
                    ex.printStackTrace();
                }
                toast(TextUtils.isEmpty(errorMsg) ? "提交失败" : errorMsg);
            }

            @Override
            public SingleResult doInBackground(SingleResult s) {
                return super.doInBackground(s);
            }

            @Override
            public void onEnd() {
                super.onEnd();
                closeProgress();
            }
        });
    }


}
